import {doGet} from "@/config/http";
import {ref, toRefs} from "vue";
import useEnumStore from "@/store/modules/enums";
import {svc} from "@/config/common";

export function useEnum(...args) {
    const res = ref({});
    return (() => {
        args.forEach((enumType, index) => {
            console.log('index', index)
            res.value[enumType] = [];
            const enums = useEnumStore().getEnums(enumType);
            if (enums) {
                res.value[enumType] = enums;
            } else {
                doGet(svc, '/common/enums', {type: enumType}, false).then(resp => {
                    res.value[enumType] = resp?.map(item => ({
                        label: item.value,
                        value: item.key,
                        elTagType: item.elTagType || 'default',
                        elTagClass: item.state === '1' ? item.elTagClass : "text-muted",
                        elTagStyle: item.elTagStyle
                    }))
                    useEnumStore().setEnums(enumType, res.value[enumType]);
                })
            }
        })
        return toRefs(res.value);
    })()
}
